import { getToken } from "./auth";
import api from "../static/api";
/*
 * 下载二进制文件
 * @Author: your name
 * @Date: 2021-01-08 17:31:42
 * @FilePath: \page\src\utils\downloadFile.js
 */

/**
 * 文件下载
 * @param {*} fileBlob  文件Blob
 * @param {*} fileName  生成的文件名
 */
export function downFile(fileBlob, fileName, callBack) {
  const blob = new Blob([fileBlob]);
  if ("download" in document.createElement("a")) {
    // 非IE下载
    const elink = document.createElement("a");
    elink.download = fileName;
    elink.style.display = "none";
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click();
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink);
  } else {
    // IE10+下载
    navigator.msSaveBlob(blob, fileName);
  }
  if (callBack) {
    callBack();
  }
}

export function getImgUrl(url, id, type) {
  return (
    api.apiOther + 
    "/plat_service/v1/api/" +
    url +
    "/" +
    id +
    "?op=download_file&name=" +
    type +
    (getToken() ? "&ak=" + getToken() : "")
  );
}

export function getUserPhoto(url, id, type) {
  return (
    api.apiUser + 
    "/plat_user/v1/api/" +
    url +
    "/" +
    id +
    "?op=download_file&name=" +
    type +
    (getToken() ? "&ak=" + getToken() : "")
  );
}

export function getVideoUrl(url) {
  return api.apiOther + "/art_video_path/" + url;
}

export function getBigImg(id) {
  return `${api.apiMap}//seis/v3//big_image/${id}/0/0/0.jpg`;
}
